// pages/newsDetails/newsDetails.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    comment:false,
    is_heart:false,
    keyboard:false,
    height:0
  },
  inputFocus(e) {
    this.setData({
      // height: e.detail.height + 50,
      height: 10,
      isInput: true,
    })
  },
  inputBlur() {
    this.setData({
      isInput: false
    })
  },
 
  focusButn: function () {
    this.setData({
      focusInput: true,
      isInput: true
    })
  },
  
  zan:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var dynamic = that.data.detail;
    var user = wx.getStorageSync('userInfo')
    app.httpRequest('dynamic/dynamic_reliable', {id: id}, 'get', false, function(res){
      var is_reliable = dynamic[index].is_reliable;
      dynamic[index].showbtn = !dynamic[index].showbtn;
      if(is_reliable == true){
        dynamic[index].is_reliable = false;
        dynamic[index].reliable_num -= 1;
        var i = dynamic[index].reliableList.indexOf(user.nickName);
        dynamic[index].reliableList.splice(i,1);
      }else{
        dynamic[index].is_reliable = true;
        dynamic[index].reliable_num += 1;
        dynamic[index].reliableList.push(user.nickName);
      }
      that.setData({
        detail:dynamic,
      })
      app.userBehavior(is_reliable ? 120 : 121,'进行了动态点赞')
    })
  },
  comment:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var dynamic = that.data.detail;
    dynamic[index].showbtn = !dynamic[index].showbtn;
    that.setData({
      detail: dynamic,
      isInput: true,
      id: id
    })
  },
  subComment:function(e){
    var that = this;
    var content = e.detail.value.content
    var id = that.data.id;
    var detail = that.data.detail;
    that.setData({
      value: '',
    })
    if(!id) return;
    if(!content){
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      })
      return;
    }
    app.httpRequest('dynamic/dynamic_comment', {id: id, content: content}, 'get', false, function(res){
      wx.showToast({
        title: res.msg,
      })
      var newCommnet = [{
        name: wx.getStorageSync('userInfo').nickName,
        comment: content
      }]
      for(var i=0; i < detail.length; i++){
        if(detail[i].dynamic_id == id){
          detail[i].commentList = detail[i].commentList.concat(newCommnet)
        }
      }
      that.setData({
        detail: detail
      })
      app.userBehavior(122,'进行了动态评论')
    })
  },
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    app.httpRequest('dynamic/detail', {dynamic_id: id}, 'get', false, function(res){
      that.setData({
        detail: res.data.detail.data,
        enterprise: res.data.enterprise,
        card: res.data.card,
        pageShow: true,
      })
    })
  },
  toindex:function(){
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  dynamicInfoShow:function(e){
    var that = this;
    var dynamic = that.data.detail;
    var index =e.currentTarget.dataset.index;
    dynamic[index].showbtn = !dynamic[index].showbtn;
    that.setData({
      detail: dynamic
    })
  },
  mesHandle: function(){
    let that = this
    that.setData({
      comment: !that.data.comment
    })
  },
  heartHandle: function () {
    let that = this
    that.setData({
      is_heart: !that.data.is_heart
    })
  },
  commentHandle: function () {
    let that = this
    that.setData({
      comment: !that.data.comment,
      keyboard: true,
    })
  },
  onPageScroll: function () {
    let that = this
    that.setData({
      keyboard: false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // /**
  //  * 用户点击右上角分享
  //  */
  onShareAppMessage: function () {
    var that = this;
    app.userBehavior(119,'分享了动态')
    var title = that.data.detail[0].content;
    var path = "/pages/dynamicDetails/dynamicDetails?share_card_id=" + wx.getStorageSync('share_card_id') + "&share_user_id=" + wx.getStorageSync('user_id')+'&id='+that.data.detail[0].dynamic_id;
    var imgurl = that.data.detail[0].images[0];
   
    return {
      title: title,
      path: path,
      imageUrl: imgurl,
      success: function (a) {
        
      },
      fail: function (a) { }
    };
  }
})